<?php
namespace control;
/**
 * 系统提示
 */
class promptClass extends adIndexClass
{
	private const page  = 'prompt'; # 权限
	private const table = 'para';       # 数据表
	private const title = '系统提示';

    /**
     * 字段初始化
     */
    public function fieldInit($fun): array
    {
        switch ($fun) {
            default:
                $tr = [
                    'paid'          => [
                        'name'  => "{l.prompt.homeSearch.paid}",
                        'width' => 300,
                    ],
                    'paName'  => [
                        'name'  => "{l.prompt.homeSearch.name}",
                        'width' => 300,

                    ],
                    'paValue'         => [
                        'name'  => "{l.prompt.homeSearch.value}",
                        'width' => 650,
                    ],
                    'list'          => [
                        'name'  => "{l.prompt.homeSearch.list}",
                        'width' => 100,
                        'sort' => 1,
                    ],
                    'time'          => [
                        'name'  => "{l.prompt.homeSearch.time}",
                        'width' => 200,
                        'sort' => 1,
                    ]
                ];
        }
        return $tr;
    }

    /*列表页-查询条件*/
    public function homeOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->tableShow([
            [
                [
                    L('prompt.home.id') => text(array("name" => "id")),
                    L('prompt.home.name')   => text(array("name" => "name")),
                    L('prompt.home.content') => text(array("name" => "content")),
                ],
                [
                    L('prompt.home.edit') => select(array("name" => "edit", "option" => ["是", "否"])),
                ]
            ]
        ],1);
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

	/*列表页~查询条件*/
	public function home()
	{
        $html = $this->homeOptions(false);
        $button = "";
        //返回
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
	}

	/*系统设置-系统提示~查询结果*/
	public function homeSearch()
	{
		//like查询数组
		$like = array(
			"paName"  => $this->post('name'),//名称
			"paValue" => $this->post('content'),//内容
		);
		//全等查询数组
		$equal = array(
			"paid" => $this->post('id'),//ID号
			"edit" => $this->post('edit'),//可编辑
		);
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'list',
            "sort" => $this->post('list_sort') ?: "asc",
            "array"   => array(),
        ));
		//获取查询结果
		$where = " paType = '提示' AND paLang = '" . lang . "'  " . search(array("like" => $like, "=" => $equal));
		$page  = $this->page(["table" => self::table, "where" => $where, "order" => $orderBy['orderBy']]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array("");
		//获取记录
		foreach ($page['array'] as $array) {
			if ($this->power(self::page, "edit") and $array['edit'] == "是") {
				$edit = "<img onclick=\"layer('" . self::page . "','detail',{id:'" . $array['paid'] . "'})\" src='" . root . "img/control/edit.svg' title='编辑' class='listIco'>";
			} else {
				$edit = "";
			}
            $liButton[] = $edit;
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
		}
		//控件
		$button = "";
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
        ));
		//返回
		$json['html'] = $page['hint'] . $button . "<form name='listForm'>" . $li . "</form>" . $page['button'];
		return json_encode($json, 320);
	}

	/*系统设置-系统提示-编辑*/
	public function detail()
	{
		$para  = query(self::table, " paid = '" . $this->post('id') . "' AND paLang = '" . lang . "' " . " AND paType='提示' ");
		$html = $this->layerEdit(array(
			"table" => array(
				array(
					"{l.prompt.detail.warmTips}" => "{l.prompt.detail.warmTipsMsg}",
				),
				array(
					"{l.prompt.detail.paName}" => $para['paName'],
				),
				array(
					must . "{l.prompt.detail.paValue}" => textarea(array("name" => "paValue", "value" => $para['paValue'])),
				),
				array(
					must . "{l.prompt.detail.pas}" => password(array("name" => "pas")),
				),
			),
		));
		return $this->layer(array("title" => "{l.prompt.detail.layerTitle}" . self::title, "height" => 360, "width" => 800, "html" => $html));
	}

	/*系统设置-系统提示-新增或更新*/
	public function detailEdit()
	{
		//赋值
		$field = array(
			"paValue" => $this->post('paValue'),   //提示值
			"time"    => date("Y-m-d H:i:s"),      //更新时间
		);
		$para  = query(self::table, " paid = '" . $this->post('id') . "' AND paLang = '" . lang . "' " . " AND paType='提示' ");
		//判断
		if (!$this->power(self::page, "edit")) {
			$json['warn'] = lang('prompt.detailEdit.powerMsg');
			// $json['warn'] = "你没有编辑的权限";
		} elseif (empty($field['paValue'])) {
			$json['warn'] = lang('prompt.detailEdit.paValueEmpty');
			// $json['warn'] = "提示值为空";
		} elseif (empty($this->post('pas'))) {
			$json['warn'] = lang('prompt.detailEdit.pasEmpty');
			// $json['warn'] = "请填写管理员登录密码";
		} elseif (verifyPwd($this->post('pas'), self::$user['password']) !== true) {
			$json['warn'] = lang('prompt.detailEdit.passwordErr');
			// $json['warn'] = "管理员登录密码输入错误";
		} elseif (empty($this->post('id'))) {
			$json['warn'] = lang('prompt.detailEdit.paidEmpty');
			// $json['warn'] = "提示ID号为空";
		} elseif (empty($para['paid'])) {
			$json['warn'] = lang('prompt.detailEdit.paidNone');
			// $json['warn'] = "未找到这条提示";
		} elseif ($para['edit'] == "否") {
			$json['warn'] = lang('prompt.detailEdit.notedit');
			// $json['warn'] = "此提示不可编辑";
		} else {
			$warn = update(array("table" => self::table, "field" => $field, "where" => " paid = '" . $this->post('id') . "'"));
			if ($warn == "更新成功") {
				//添加日志
				$text = $this->control['name'] . "更新了" . self::title . "：" . $para['paName'] . "-" . $field['paValue'];
				$this->logText(array("type" => self::page, "text" => $text));
				//返回
				$json['warn']     = 2;
				$_SESSION['warn'] = $warn;
			} else {
				$json['warn'] = $warn;
			}
		}
		return json_encode($json, 320);
	}
}
